﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using crmservice;
using System.Text;

/// <summary>
/// Contains static helper methods
/// </summary>
public class Utilities
{

    public static Picklist RadioButtonToPickList(RadioButton radButton1, RadioButton radButton2)
    {
        Picklist PL = new Picklist();
        if (radButton1.Checked)
            PL.Value = 1;
        else if (radButton2.Checked)
            PL.Value = 2;
        else
            return null;
        return PL;
    }

    public static void LoadStateDropDownList(CrmService crm, ref DropDownList ddlState)
    {
        QueryExpression query = new QueryExpression();
        query.EntityName = "oasys_usstate";
        query.ColumnSet = new AllColumns();
        BusinessEntityCollection States = crm.RetrieveMultiple(query);
        var SortedStates = from oasys_usstate s in States.BusinessEntities orderby s.oasys_sortorderoverride, s.oasys_name select s;
        foreach (oasys_usstate State in SortedStates)
        {
            ddlState.Items.Add(State.oasys_name);
        }
    }

    public static void LoadEthnicityDropDownList(CrmService crm, ref DropDownList ddlEthnicity)
    {
        QueryExpression query = new QueryExpression();
        query.EntityName = "oasys_ethnicity";
        query.ColumnSet = new AllColumns();
        BusinessEntityCollection Ethnicities = crm.RetrieveMultiple(query);
        var SortedEthnicities = from oasys_ethnicity e in Ethnicities.BusinessEntities orderby e.oasys_name select e;
        ddlEthnicity.Items.Add("");
        foreach (oasys_ethnicity Ethnicity in SortedEthnicities)
        {
            ddlEthnicity.Items.Add(Ethnicity.oasys_name);
        }
    }

    public static string BuildBoardJavascript(CrmService crm)
    {
        StringBuilder Script = new StringBuilder("<script language='javascript'>\n");
        Script.AppendLine("var SecBoards = new Array();");
        ColumnSet cols = new ColumnSet();
        cols.Attributes = new string[] { "oasys_boardsandcommissionsmasterlistid", "oasys_boardandcommissionbysecretariatid", 
            "oasys_displayname", "oasys_activestartdate", "oasys_activeenddate" };
        QueryExpression query = new QueryExpression();
        query.EntityName = "oasys_boardsandcommissionsmasterlist";
        query.ColumnSet = cols;
        BusinessEntityCollection boards = crm.RetrieveMultiple(query);
        DateTime Today = DateTime.Now.Date;
        var SortedBoards = from oasys_boardsandcommissionsmasterlist b in boards.BusinessEntities
                           where ((b.oasys_activestartdate == null) || (DateTime.Parse(b.oasys_activestartdate.date) <= Today))
                                && ((b.oasys_activeenddate == null) || (DateTime.Parse(b.oasys_activeenddate.date) >= Today))
                           orderby b.oasys_boardandcommissionbysecretariatid.name, b.oasys_displayname select b;
        int SecIdx = 0;
        int BoardIdx = 0;
        string CurrSec = "";
        foreach (oasys_boardsandcommissionsmasterlist board in SortedBoards)
        {
            if (board.oasys_boardandcommissionbysecretariatid.name != CurrSec)
            {
                if (CurrSec != "")
                {
                    Script.AppendFormat("Secretariats[{0}] = new Secretariat(\"{1}\", SecBoards);\n", SecIdx++, CurrSec);
                    Script.AppendLine("SecBoards = new Array();");
                }
                CurrSec = board.oasys_boardandcommissionbysecretariatid.name;
                BoardIdx = 0;
            }
            Script.AppendFormat("SecBoards[{0}] = new Board(\"{1}\", \"{2}\");\n", BoardIdx++, board.oasys_boardsandcommissionsmasterlistid.Value.ToString(), board.oasys_displayname);
        }
        Script.AppendFormat("Secretariats[{0}] = new Secretariat(\"{1}\", SecBoards);\n", SecIdx, CurrSec);
        Script.AppendLine("var lstSecretariats = document.getElementById('lstSecretariats')");
        Script.AppendLine("var lstBoards = document.getElementById('lstBoards')");
        Script.AppendLine("ShowSecretariats();");
        Script.AppendLine("ShowBoards(lstSecretariats, lstBoards);");
        Script.AppendLine("</script>");
        return Script.ToString();
    }

    public static string BuildFullName(string firstName, string middleName, string lastName)
    {
        StringBuilder sbFullName = new StringBuilder(firstName);
        if (middleName.Trim() != "")
            sbFullName.AppendFormat(" {0}", middleName);
        sbFullName.AppendFormat(" {0}", lastName);
        return sbFullName.ToString();
    }

}
